package top.codedance.iotp.client.net.handler.action;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.client.ClientApplication;
import top.codedance.iotp.client.net.handler.AdapterController;
import top.codedance.iotp.common.constent.Command;
import top.codedance.iotp.common.entity.CommandTeam;
import top.codedance.iotp.common.entity.PluginResultEntity;
import top.codedance.iotp.common.executer.CommonClientPluginExecuter;
import top.codedance.iotp.common.util.OSinfo;
import top.codedance.iotp.common.util.SystemUtil;
import org.apache.log4j.Logger;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class OprationController extends AdapterController {
    private short command = -1;

    public OprationController(short command) {
        this.command = command;
    }

    private static Logger logger = ClientApplication.getLogger(OprationController.class);

    public String doSubHandler(JSONObject data) {
        try {
            PluginResultEntity pluginResultEntity = new PluginResultEntity();
            short plugin = command;
            if(data != null) {
                final byte[] body = data.getJSONObject("client").toJSONString().getBytes(StandardCharsets.UTF_8);
                Set<Map.Entry<CommandTeam, CommonClientPluginExecuter>> entries2 = ClientApplication.syncExcuters.entrySet();
                boolean isSuccess = false;
                for (Map.Entry<CommandTeam, CommonClientPluginExecuter> entry : entries2) {
                    CommandTeam commandTeam = entry.getKey();
                    if (plugin == commandTeam.getRequestCommand()) {
                        final CommonClientPluginExecuter commonExecuter = entry.getValue();
                        commonExecuter.setIniConfig(ClientApplication.ini);
                        pluginResultEntity = commonExecuter.exec(body, UUID.randomUUID().toString());
                        isSuccess = true;
                        break;
                    }
                }
                if (!isSuccess) {
                    pluginResultEntity.setOk(false);
                    pluginResultEntity.setMsg("Unsupported plugin access.");
                }
            }else{
                if (plugin == Command.NONE) {
                    JSONObject jo = new JSONObject();
                    jo.put("deviceId", ClientApplication.DEVICE_ID);
                    jo.put("load", SystemUtil.getSystemUsage());
                    jo.put("os", OSinfo.getOSname());
                    pluginResultEntity.setOk(true);
                    pluginResultEntity.setContext(jo);
                } else {
                    pluginResultEntity.setOk(false);
                    pluginResultEntity.setMsg("Unsupported plugin access.");
                }
            }
            return okResponse(pluginResultEntity);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return response500( "Programe exception");
        }
    }
}
